package com.movcart.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.movcart.emuns.Line;
import com.movcart.entities.CommonResult;
import com.movcart.service.LineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-12-10
 */
@Controller
@RequestMapping("/line")
@Tag(name = "线路")
public class LineController {
    @Resource
    private LineService lineService;

    @Value("${movcart.line.document-pre}")
    private String lineDocument;

    //获取所有线路信息
    @GetMapping("/all")
    @ResponseBody
    @Operation(summary = "获取所有线路信息", description = "() => 获取所有线路信息")
    public CommonResult getAllLine() {
        List<Line> allLines = lineService.list();
        return new CommonResult(200, "获取所有路线成功", allLines);
    }

    //根据id获取线路信息
    @GetMapping("/id")
    @ResponseBody
    @Operation(summary = "根据id获取线路信息", description = "(Integer id) => 根据id获取线路信息")
    @Parameters({
            //参数效验
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult getLineById(@RequestParam Integer id) {
        Line line = lineService.getById(id);
        if (line != null) {
            return new CommonResult(200, "根据id:" + id + ",获取路线成功", line);
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到路线");
        }
    }

    //根据id获取线路文件
    @GetMapping("/idDoc")
    @Operation(summary = "根据id获取线路文件", description = "(Integer id) => 获取线路文件")
    @Parameters({
            //参数效验
            @Parameter(name = "id", description = "id", required = true),
    })
    public ResponseEntity<InputStreamResource> getLineDocumentById(@RequestParam Integer id) throws FileNotFoundException {
        Line line = lineService.getById(id);
        if (line != null) {
            String fileName = line.getFileName();

            File file = new File(lineDocument + fileName);
            InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

            val lineFile = ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .contentLength(file.length())
                    .body(resource);
            return lineFile;
//            return new CommonResult(200, "根据id:" + id + ",获取路线文件成功", body);
        } else {
//            return new CommonResult(404, "根据id:" + id + ",未找到路线");
            return null;
        }
    }


    //创建新小车
    @PostMapping("/")
    @Operation(summary = "创建新线路", description = "(String describe0,String fileName) => 创建新线路")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "describe0", description = "线路描述", required = true),
            @Parameter(name = "fileName", description = "线路文件名", required = true),
    })
    public CommonResult createCart(@RequestParam(required = true)String describe0,
                                   @RequestParam(required = true)String fileName) {
        lineService.save(new Line(null,describe0,fileName));
        return new CommonResult(200, "创建线路成功");
    }

    //根据id更新小车信息
    @PostMapping("/id")
    @Operation(summary = "根据id更新线路信息", description = "(Integer id,?String describe0,?String fileName) => 更新线路信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "describe0", description = "线路描述", required = false),
            @Parameter(name = "fileName", description = "线路文件名", required = false),
    })
    public CommonResult getCartById(@RequestParam Integer id,
                                    @RequestParam(required = false) String describe0,
                                    @RequestParam(required = false) String fileName
                                    ) {
        Line line = lineService.getById(id);
        if (line != null) {
            lineService.updateById(new Line(id,describe0,fileName));
            return new CommonResult(200, "根据id:" + id + ",更新线路信息成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到线路");
        }
    }

    //根据id移除小车信息
    @GetMapping("/id")
    @Operation(summary = "根据id移除线路信息", description = "(Integer id) => 根据id移除线路信息")
    @Parameters({
            @Parameter(name = "id", description = "id", required = true),
    })
    public CommonResult removeCartById(@RequestParam(required = true) Integer id) {
        Line line = lineService.getById(id);
        if (line != null) {
            lineService.removeById(id);
            return new CommonResult(200, "根据id:" + id + ",移除线路成功");
        } else {
            return new CommonResult(404, "根据id:" + id + ",未找到线路");
        }
    }

}
